import requests

class school_naire():
    def getHTMLText(self, url, token):#get请求
        try:
            r = requests.get(url, headers={
    'Authorization': 'Bearer ' + token
   })
            r.raise_for_status()
            r.encoding = r.apparent_encoding
            return r.text
        except:
            return ''

    def Schdata2List(self, n_id, result):#{ 'n_id':'' , n_name :'' , s_id :'' , 's_name' :''  }
        S_list = []
        S_dict = {}
        for s in result:
            S_dict['n_id'] = n_id
            S_dict['s_id'] = s['id']
            S_dict['s_name'] = s['name']
            S_list.append(S_dict)
            S_dict = {}
        return S_list

    def List2Dict(self, List):
        S_Dict = {}
        S_dict = {'s_name': '','n_id': []}
        schoolDict = {}
        _schoolDict = {}
        for s in List:
            S_dict['s_name'] = s['s_name']
            schoolDict[s['s_id']] = s['s_name']
            _schoolDict[s['s_name']] = s['s_id']
            try:
                S_dict['n_id'] = S_Dict[s['s_id']]['n_id']+[s['n_id']]
            except:
                S_dict['n_id'].append(s['n_id'])
            S_Dict[s['s_id']] = S_dict
            S_dict = {'s_name': '', 'n_id': []}

        return S_Dict, tuple(S_Dict.keys()), schoolDict, _schoolDict

class NaireJson():
    def getHTMLText(self, url, token):  # get请求
        try:
            r = requests.get(url, headers={
                'Authorization': 'Bearer ' + token
            })
            r.raise_for_status()
            r.encoding = r.apparent_encoding
            return r.text
        except:
            return ''

    def naire2json(self, list):
        n_idList = []
        titleList = []
        naireDict = {}
        _naireDict = {}
        for dict in list:
            n_idList.append(dict['id'])
            titleList.append(dict['title'])
            naireDict[dict['id']] = dict['title']
            _naireDict[f"{dict['id']}_{dict['title']}"] = dict['id']
        return n_idList, titleList, naireDict, _naireDict


if __name__ == '__main__':
    sn = school_naire()
    token = 00
    naire = [100,107]
    depId = 20
    List = []
    for n_id in naire:
        url = "https://api.cqtrust.cn/survey/index.php/dep/%d"% n_id
        response = eval(sn.getHTMLText(url, token))
        result = response['result']
        list = sn.Schdata2List(n_id, result)
        List = List + list
    # List = []
    # for i in range(0,2):
    #     if i == 1:
    #         n_id = 100
    #         n_name = '第100套'
    #         result = [
    #             {'id': 10,
    #              'name': '第10所'
    #         },
    #             {'id': 20,
    #              'name': '第20所'
    #              },
    #         ]
    #         list = sn.Schdata2List(n_id,result)
    #         List = List + list
    #     else:
    #         n_id = 200
    #         n_name = '第200套'
    #         result = [
    #             {'id': 20,
    #              'name': '第20所'
    #              },
    #             {'id': 30,
    #              'name': '第30所'
    #              },
    #         ]
    #         list = sn.Schdata2List(n_id, result)
    #         List = List + list
    S_Dict,s_idList,schoolDict,_schoolDict = sn.List2Dict(List)
    # print(s_idList)
    result = S_Dict[depId]
    s_name = result['s_name']
    n_id = result['n_id']
    # print(s_name,n_id)
